﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.PagerControl;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.IO;
using System.Resources;

public partial class admin_CountryList : SitePage
{
    #region ViewState
    public int count = 0;
    public string paraURL
    {
        get
        {
            object o = ViewState["paraURL"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["paraURL"] = value; }

    }
    public int F_PageIndex
    {
        get
        {
            object o = ViewState["F_PageIndex"];
            if (o != null) return (int)o;
            return 0;
        }
        set { ViewState["F_PageIndex"] = value; }
    }
    public int F_PageSize
    {
        get
        {
            object o = ViewState["F_PageSize"];
            if (o != null) return (int)o;
            return 0;
        }
        set { ViewState["F_PageSize"] = value; }
    }
    public string F_SortField
    {
        get
        {
            object o = ViewState["F_SortField"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["F_SortField"] = value; }

    }
    public string F_SortExpression
    {
        get
        {
            object o = ViewState["F_SortExpression"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["F_SortExpression"] = value; }

    }
    #endregion

    #region Init,Loads
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            clsBindControl.BindBooleanCombobox(F_ddlIsActive);
            GetParamater();
            LoadData();
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    #endregion

    #region Events
    protected void Pager2_PageIndexChanging(object obj, PageIndexChangeEventArgs e)
    {
        this.F_PageIndex = e.PageIndex;
        LoadData();
    }
    protected void Pager2_PageSizeChanging(object obj, PageSizeChangeEventArgs e)
    {
        this.F_PageIndex = 1;
        this.F_PageSize = Pager2.PageSize = Pager1.PageSize = e.PageSize;
        LoadData();
    }
    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Country objData = (Country)e.Item.DataItem;
            if (objData == null)
                return;
            hidID.Value += objData.CountryID.ToString() + ";";

            //image button IsActive
            ImageButton imgButton = (ImageButton)e.Item.FindControl("imbActive");
            imgButton.CommandArgument = objData.CountryID.ToString();
            if (objData.IsActive == true)
            {
                imgButton.ImageUrl = clsString.ACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_DEACTIVE;
                imgButton.CommandName = "DeActive";
            }
            else
            {
                imgButton.ImageUrl = clsString.DEACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_ACTIVE;
                imgButton.CommandName = "Active";
            }
            //image button Order
            imgButton = (ImageButton)e.Item.FindControl("imbOrderDown");
            if (e.Item.ItemIndex + 1 == count)
                imgButton.Visible = false;
            else
            {
                imgButton.ToolTip = Resources.Resource.TIP_MOVEDOWN;
                imgButton.CommandArgument = objData.CountryID.ToString();
                imgButton.ImageUrl = clsString.ORDERDOWN_IMAGE_URL;
            }
            imgButton = (ImageButton)e.Item.FindControl("imbOrderUp");
            if (e.Item.ItemIndex == 0)
                imgButton.Visible = false;
            else
            {
                imgButton.CommandArgument = objData.CountryID.ToString();
                imgButton.ToolTip = Resources.Resource.TIP_MOVEUP;
                imgButton.ImageUrl = clsString.ORDERUP_IMAGE_URL; ;
                imgButton.Visible = true;
            }

            imgButton = (ImageButton)e.Item.FindControl("imbDelete");
            imgButton.ToolTip = Resources.Resource.TIP_DELETE;
            imgButton.Attributes.Add("onclick", "return confirm('" + Resources.Resource.MSG_CONFIRM_DELETE + "');");
            imgButton.CommandArgument = objData.CountryID.ToString();
            imgButton.ImageUrl = clsString.DELETE_IMAGE_URL;
        }
    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "SortOrder")
        {
            SortList(e.CommandArgument.ToString());

        }
        else
        {
            int id = clsConvert.Int(e.CommandArgument.ToString());
            switch (e.CommandName)
            {
                case "OrderUp":
                    {
                        OrderUp(id);
                        break;
                    }
                case "OrderDown":
                    {
                        OrderDown(id);
                        break;
                    }
                case "Active":
                    {
                        Active(id);
                        break;
                    }
                case "DeActive":
                    {
                        DeActive(id);
                        break;
                    }
                case "Delete":
                    {
                        Delete(id);
                        LoadData();
                        break;
                    }
            }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("CountryList.aspx");
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("CountryDetail.aspx");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.F_SortField = "SortOrder";
        this.F_SortExpression = "ASC";
        this.F_PageIndex = 1;
        LoadData();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string[] arr = hidIDSelect.Value.Split(';');
        for (int i = 0; i < arr.Length - 1; i++)
        {
            if (arr[i].ToString() != string.Empty)
                Delete(clsConvert.Int(arr[i].ToString()));
        }
        LoadData();
    }
    #endregion

    #region Methods
    private void SortList(string F_SortField)
    {
        if (this.F_SortField == F_SortField)
        {
            if (this.F_SortExpression == "ASC")
                this.F_SortExpression = "DESC";
            else this.F_SortExpression = "ASC";
        }
        else
        {
            this.F_SortField = F_SortField;
            this.F_SortExpression = "ASC";
        }
        LoadData();
    }
    private void OrderDown(int id)
    {
        busCountry objBus = new busCountry();
        if (objBus.OrderDown(id))
            LoadData();
    }
    private void OrderUp(int id)
    {
        busCountry objBus = new busCountry();
        if (objBus.OrderUp(id))
            LoadData();
    }
    private void Active(int id)
    {
        busCountry objBus = new busCountry();
        if (objBus.Active(id))
            LoadData();
    }
    private void DeActive(int id)
    {
        busCountry objBus = new busCountry();
        if (objBus.DeActive(id))
            LoadData();
    }
    private void Delete(int id)
    {
        if (id <= 0)
            return;
        busCountry objBus = new busCountry();
        objBus.Delete(id);
    }
    private void LoadData()
    {
        paraURL = GetPageParams(FilterFieldType.All);
        hidID.Value = string.Empty;
        CountryCollection lstResult = null;
        //get Condition
        busCountry objBus = new busCountry();
        Country objCountry = new Country();
        objCountry.CurrentPage = this.F_PageIndex;
        objCountry.OrderBy = this.F_SortField;
        objCountry.OrderDirection = this.F_SortExpression;
        objCountry.PageSize = Pager1.PageSize;
        string condition = " 1=1 ";
        if (F_txtCode.Text != string.Empty)
            condition += " and c.CountryCode like N'%" + F_txtCode.Text + "%'";
        if (F_txtName.Text != string.Empty)
            condition += " and c.CountryName like N'%" + F_txtName.Text + "%'";
        if (F_ddlIsActive.SelectedValue != string.Empty)
            condition += " and c.IsActive=" + F_ddlIsActive.SelectedValue;
        objCountry.Condition = condition;
        lstResult = objBus.List(objCountry);
        if (lstResult != null && lstResult.Count > 0)
        {
            count = lstResult.Count;
            rptData.DataSource = lstResult;
            divGrid.Visible = true;
            divEmplty.Visible = false;
        }
        else
        {
            rptData.DataSource = null;
            divGrid.Visible = false;
            divEmplty.Visible = true;
            divEmplty.InnerHtml = Resources.Resource.MSG_EMPTY;
        }
        rptData.DataBind();
        Pager1.SetPaging(this.F_PageIndex, count, objCountry.TotalRecords);
        Pager2.SetPaging(this.F_PageIndex, count, objCountry.TotalRecords);
    }
    private void GetParamater()
    {
        F_txtName.Text = clsQueryString.GetString("F_txtName");
        F_txtCode.Text = clsQueryString.GetString("F_txtCode");
        this.F_SortField = clsQueryString.GetString("F_SortField");
        if (this.F_SortField == string.Empty)
            this.F_SortField = busCountry.COUNTRYNAME;
        this.F_SortExpression = clsQueryString.GetString("F_SortExpression");
        if (this.F_SortExpression == string.Empty)
            this.F_SortExpression = "ASC";
        F_PageIndex = clsQueryString.GetInt("F_PageIndex");
        F_PageSize = clsQueryString.GetInt("F_PageSize");
        if (F_PageSize < 1)
            F_PageSize = clsConfigData.DEFAULT_ADMIN_PAGE_SIZE;
        if (F_PageIndex < 1)
            F_PageIndex = 1;
        this.Pager2.PageSize = this.Pager1.PageSize = F_PageSize;
        F_ddlIsActive.SelectedValue = clsQueryString.GetString("F_ddlIsActive");
    }
    #endregion

}